package com.symone.webcms.controller.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.common.Constants;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.*;
import com.symone.webcms.model.vo.MessageVO;
import com.symone.webcms.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/cms/my")
public class MyController extends BaseController {

    @Autowired
    public MessageService messageService;
    @Autowired
    public UserService userService;
    @Autowired
    public CompanyService companyService;
    @Autowired
    public SiteService siteService;
    @Autowired
    public ConfigService configService;

    @RequestMapping(value = "/message/index", method = RequestMethod.GET)
    public ModelAndView messageBox() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/message/index");
        return mv;
    }

    @RequestMapping(value = "/message/add", method = RequestMethod.GET)
    public ModelAndView messageAdd() {
        ModelAndView mv = new ModelAndView();
        List<User> users = userService.selectList(new EntityWrapper<User>().eq("deleted",0));
        mv.addObject("users",users);
        mv.setViewName("/my/message/form");
        return mv;
    }

    @RequestMapping(value = "/message/info/{id}", method = RequestMethod.GET)
    public ModelAndView messageInfo(@PathVariable("id") @Valid Long id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/message/info");
        mv.addObject("message",messageService.getMessageVOById(id,getSessionUser().getId()));
        return mv;
    }

    @RequestMapping(value = "/notification/index", method = RequestMethod.GET)
    public ModelAndView notificationList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/notification/index");
        return mv;
    }

    @RequestMapping(value = "/notification/setting", method = RequestMethod.GET)
    public ModelAndView notificationSetting() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/notification/setting");
        return mv;
    }

    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public ModelAndView profile() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/profile");
        mv.addObject("user",getSessionUser());
        return mv;
    }

    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public ModelAndView password() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/password");
        mv.addObject("user",getSessionUser());
        return mv;
    }

    @RequestMapping(value = "/setting", method = RequestMethod.GET)
    public ModelAndView setting() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/setting");
        return mv;
    }

    @RequestMapping(value = "/language/{id}", method = RequestMethod.GET)
    public String language(@PathVariable("id") @Valid Integer id) {
        Language language = getLanguageById(id);
        SecurityUtils.getSubject().getSession().setAttribute(Constants.CURRENT_LANGUAGE,language);
        return "redirect:/cms/framework";
    }


    @RequestMapping(value = "/company/index", method = RequestMethod.GET)
    public ModelAndView company() {
        Language language = (Language) getSession().getAttribute(Constants.CURRENT_LANGUAGE);
        Company company = companyService.selectById(language.getCode());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/company/index");
        if(company == null) {
            company = new Company();
        }
        mv.addObject("company", company);
        mv.addObject("currentlanguage", getSessionLanguage());
        return mv;
    }

    @RequestMapping(value = "/site/index", method = RequestMethod.GET)
    public ModelAndView site() {
        Language language = (Language) getSession().getAttribute(Constants.CURRENT_LANGUAGE);
        Wrapper<Site> wrapper = new EntityWrapper<Site>().eq("lang",language.getCode());
        Site site = siteService.selectOne(wrapper);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/site/index");
        if(site == null) {
            site = new Site();
        }
        mv.addObject("site", site);
        mv.addObject("currentlanguage", getSessionLanguage());
        return mv;
    }

    @RequestMapping(value = "/smtp/index", method = RequestMethod.GET)
    public ModelAndView smtp() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/my/smtp/index");
        mv.addObject("configs", configList("email"));
        return mv;
    }

    private List<Config> configList(String groupName){
        return configService.selectList(new EntityWrapper<Config>().where("deleted = 0 and code like CONCAT('"+groupName+"','%' ) "));
    }

}
